#!/bin/bash

LayoutLibName=$1
LayoutCellName=$2
ScheLibName=$3
ScheCellName=$4

###### rule path
lvs_rule="/projects/analog_ip/workspace/xwqiu/cl/tacoma6/verify/LVS/DFM_LVS_RC_CALIBRE_N6_1p13M_1X1Xa1Ya5Y2Yy2Z_ALRDL.1.0b"
run_path="/projects/analog_ip/workspace/xwqiu/cl/tacoma6/verify/LVS"
virtuoso_run_path="/projects/analog_ip/workspace/xwqiu/cl/tacoma6/cadence"

###### check output path
if [[ -d "${run_path}/run_LVS" ]]; then
	echo "${run_path}/run_LVS is exists."
else
	echo "${run_path}/run_LVS not exist."
        mkdir run_LVS
fi

if [[ -d "${run_path}/GDS" ]]; then
	echo "${run_path}/GDS is exists."
else
	echo "${run_path}/GDS not exist."
        mkdir GDS
fi

###### out GDS file
gds_strmfile="${run_path}/GDS/${LayoutCellName}.gds"
strmout_log="${run_path}/GDS/strmOut.log"

cd ${virtuoso_run_path}

strmout -library ${LayoutLibName} \
        -strmFile ${gds_strmfile} \
        -topCell ${LayoutCellName} \
        -view layout \
        -logFile ${strmout_log} \
        -layerMap /projects/analog_ip/workspace/hzou/cl/tacoma6/cadence/layer.map \
        -enableColoring \
        -convertPcellPin geometry | tee ./strmout.log

echo "GDS STRMOUT SUCCESS"

###### out SPI file
netlistFilename="${ScheCellName}.netlist"
cdslib="/projects/analog_ip/workspace/xwqiu/cl/tacoma6/cadence/cds.lib"
run_dir="/projects/analog_ip/workspace/xwqiu/cl/tacoma6/verify/LVS"
viewName="schematic"

echo_f() {
	echo "simLibName = \"${ScheLibName}\"" >> ${run_dir}/si.env
	echo "simCellName = \"${ScheCellName}\"" >> ${run_dir}/si.env
	echo "simViewName = \"${viewName}\"" >> ${run_dir}/si.env
	echo "hnlNetlistFileName = \"${netlistFilename}\"" >> ${run_dir}/si.env
	
	si ${run_dir} -batch -command netlist -cdslib ${cdslib}

	cd ${run_dir}
	if [[ -d "${run_dir}/CDL" ]]; then
		mv ./${netlistFilename} ./CDL/${netlistFilename}
	else
		mkdir ./CDL
		mv ./${netlistFilename} ./CDL/${netlistFilename}
	fi
}

create_si_conf() {
	path=$1
    echo "create si.conf!!"
	echo "simSimulator = \"auCdl\"" > ${path}/si.conf
	echo "simNotIncremental = 't" >> ${path}/si.conf
	echo "simReNetlistAll = nil" >> ${path}/si.conf
	echo "simViewList = '(\"auCdl\" \"schematic\")" >> ${path}/si.conf
	echo "simStopList = '(\"auCdl\")" >> ${path}/si.conf
	echo "resistorModel = \"\"" >> ${path}/si.conf
	echo "shortRES = 2000.0" >> ${path}/si.conf
	echo "preserveRES = 't" >> ${path}/si.conf
	echo "checkRESVAL = 't" >> ${path}/si.conf
	echo "checkRESSIZE = 'nil" >> ${path}/si.conf
	echo "preserveCAP = 't" >> ${path}/si.conf
	echo "checkCAPVAL = 't" >> ${path}/si.conf
	echo "checkCAPAREA = 'nil" >> ${path}/si.conf
	echo "preserveDIO = 't" >> ${path}/si.conf
	echo "checkDIOAREA = 't" >> ${path}/si.conf
	echo "checkDIOPERI = 't" >> ${path}/si.conf
	echo "checkCAPPERI = 'nil" >> ${path}/si.conf
	echo "simPrintInhConnAttributes = 'nil" >> ${path}/si.conf
	echo "checkScale = \"meter\"" >> ${path}/si.conf
	echo "checkLDD = 'nil" >> ${path}/si.conf
	echo "pinMAP = 'nil" >> ${path}/si.conf
	echo "preserveBangInNetlist = 'nil" >> ${path}/si.conf
	echo "shrinkFACTOR = 0.0" >> ${path}/si.conf
	echo "globalPowerSig = \"\"" >> ${path}/si.conf
	echo "globalGndSig = \"\"" >> ${path}/si.conf
	echo "displayPININFO = 't" >> ${path}/si.conf
	echo "preserveALL = 't" >> ${path}/si.conf
	echo "setEQUIV = \"\"" >> ${path}/si.conf
	echo "incFILE = \"/pdk/tsmc/CLN06FF/ams/CLN6FF/tsmcN6/../Calibre/lvs/source.added\"" >> ${path}/si.conf
	echo "auCdlDefNetlistProc = \"ansCdlSubcktCall\"" >> ${path}/si.conf
}

if [[ -f "${run_dir}/si.conf" ]]; then
	echo "Exists"
	cp ${run_dir}/si.conf ${run_dir}/si.env
	echo_f
else
	echo "Not Exists"
	create_si_conf ${run_dir}
	echo "Have created si.env Now!"


	cp ${run_dir}/si.conf ${run_dir}/si.env
	echo_f
fi


###### run LVS
cd ${run_dir}

echo "LAYOUT PRIMARY '${LayoutCellName}'" > ./LVS.cal
echo "LAYOUT PATH '${gds_strmfile}'" >> ./LVS.cal
echo "LAYOUT SYSTEM GDSII" >> ./LVS.cal

echo "SOURCE PRIMARY '${ScheCellName}'" >> ./LVS.cal
echo "SOURCE PATH '${run_dir}/CDL/${netlistFilename}'" >> ./LVS.cal
echo "SOURCE SYSTEM SPICE" >> ./LVS.cal

echo "INCLUDE '${lvs_rule}'" >> ./LVS.cal

echo "LVS POWER NAME 'VDD' 'VDD?' 'VPP'" >> ./LVS.cal
echo "LVS GROUND NAMe 'VSS' 'GNDC' 'VSS?' 'VBB'" >> ./LVS.cal

echo "LAYOUT RENAME TEXT \"_\\\\[_<_g\" \"_\\\\]_>_g\"" >> ./LVS.cal


cd ${run_dir}/run_LVS
/apps/mentor/Calibre_2019.3_25.15/aoi_cal_2019.3_25.15/bin/calibre -lvs -hier -hyper -turbo 32 ../LVS.cal | tee DRC.log